package tr.edu.metu.is550.isell.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import com.google.android.maps.*;
import tr.edu.metu.is550.isell.R;

import java.util.List;

public class MapsActivity extends MapActivity {

    class MapOverlay extends Overlay {
        GeoPoint p;

        MapOverlay(GeoPoint point) {
            this.p = point;
        }

        public void setP(GeoPoint p) {
            this.p = p;
        }

        public GeoPoint getP() {
            return p;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView,
                            boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);

            if (p != null) {
                //---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(p, screenPts);

                //---add the marker---
                Bitmap bmp = BitmapFactory.decodeResource(
                        getResources(), com.example.R.drawable.pushpin);
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            }
            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {
            if (event.getAction() == 1) {
                GeoPoint p = mapView.getProjection().fromPixels(
                        (int) event.getX(),
                        (int) event.getY());

                this.setP(p);
                mapView.invalidate();
            }
            return false;
        }
    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        final MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        final MapController controller = mapView.getController();
        controller.setZoom(15);


        final Button btn = (Button) findViewById(R.id.btnOk);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                MapOverlay mapOverlay = (MapOverlay) mapView.getOverlays().get(0);
                intent.putExtra("lat", (Double) (mapOverlay.getP().getLatitudeE6() / 1E6));
                intent.putExtra("lon", (Double) (mapOverlay.getP().getLongitudeE6() / 1E6));
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });


        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
                controller.animateTo(geoPoint);
                List<Overlay> overlays = mapView.getOverlays();
                overlays.clear();
                overlays.add(new MapOverlay(geoPoint));
                mapView.invalidate();
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        });

        List<Overlay> overlays = mapView.getOverlays();
        overlays.clear();
        overlays.add(new MapOverlay(mapView.getMapCenter()));
        mapView.invalidate();
    }


    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}
